// ChildView.cpp : implementation of the CChildView class
//

#include "stdafx.h"
#include "TeaCups.h"
#include "ChildView.h"
#include <sstream>
#include <time.h>
#include "mmsystem.h"

#include "graphics/GrTransform.h"
/*#ifdef _DEBUG
#define new DEBUG_NEW
#endif
*/

// CChildView
inline double random(double fm, double to)
{
    return fm + (to - fm) * double(rand()) / double(RAND_MAX);
}
CChildView::CChildView()
{
    srand( (unsigned)time( NULL ) );
    SetDoubleBuffer(true);

    m_firstdraw = true;
    m_run = false;
    timeBeginPeriod(1);

	_game = new CGameManager();
	_camera.Set(30, 20, 30, 10, 0, 10, 1, 0, 0);
	_camera.MouseMode(CGrCamera::PITCHYAW);
	_game->AddObject(&_heli);
	//_game->setPlayer(&_heli);
	_heli.setPosition(0, 1, 0);
	_score=0;

	_renderGame = true;

	m_particles.Clear();
    m_particles.SetGravity(true);
	m_particles.SetDecay(true);
	m_particles.SetEmission(true);

    /*for(int i=0;  i<20000;  i++)
    {
        CGrVector p(random(-5, 5),       // x
                   0,      // y
                   random(-5, 5));      // z

        float color[] = {(float)random(0, 0.5), // r
                       (float)random(0, 0.5),   // g
                       (float)random(0, 0.5) }; // b

        CGrVector v(0, 0, 0, 0);
        m_particles.LoadParticle(p, v, color);
    }*/
}

CChildView::~CChildView()
{
	KillTimer(1);
	delete _game;
}


BEGIN_MESSAGE_MAP(CChildView, COpenGLWnd)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_KEYDOWN()
	ON_WM_TIMER()
	ON_COMMAND(ID_FILE_RENDERGAME, &CChildView::OnFileRendergame)
	ON_UPDATE_COMMAND_UI(ID_FILE_RENDERGAME, &CChildView::OnUpdateFileRendergame)
END_MESSAGE_MAP()



// CChildView message handlers

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
	if (!COpenGLWnd::PreCreateWindow(cs))
		return FALSE;

	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.style &= ~WS_BORDER;
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
		::LoadCursor(NULL, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1), NULL);

	return TRUE;
}

void CChildView::OnGLDraw(CDC* pDC)
{
	if (_heli.getHostages() > 30)
	{
		_renderGame = false;
		_heli.addSavedHostages(-30);
	}

	if(m_firstdraw)
    {
        SetTimer(1, 30, NULL);
        m_firstdraw = false;
        m_lasttime = timeGetTime();
    }

    // How much time as passed since the last draw?
    DWORD newtime = timeGetTime();
    double delta = (newtime - m_lasttime) * 0.001;
    m_lasttime = newtime;

	glClearColor(0.f, 0.f, 0.f, 0.f);
	glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

	int width, height;
	GetSize(width, height);
	double aspectRatio = double(height) / double(width);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(35., //vertical FoV
		aspectRatio,
		1.0, //near clip
		100.0); //far clip

	glEnable(GL_DEPTH_TEST);
	glCullFace(GL_BACK);
	glEnable(GL_CULL_FACE);	

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	_camera.gluLookAt();

	glColor3d(1., 0., 0.);

	DrawAxis();

	if (_renderGame)
	{
		//update the actual game
		_game->Update();
		_game->Render();
		_game->CheckCollision(&_heli);
	}
	else
	{
		m_particles.Update(delta);
		m_particles.Render(width, height);
		m_particles.Display(width, height);
	}


	Invalidate(); //find a new way to update the game and screen later...
}

void CChildView::OnLButtonDown(UINT nFlags, CPoint point)
{
	_camera.MouseDown(point.x, point.y);

	COpenGLWnd::OnLButtonDown(nFlags, point);
}

void CChildView::OnMouseMove(UINT nFlags, CPoint point)
{
	if ((nFlags & MK_LBUTTON) || (nFlags & MK_RBUTTON))
	{
		_camera.MouseMove(point.x, point.y);
		Invalidate();
	}

	COpenGLWnd::OnMouseMove(nFlags, point);
}

void CChildView::DrawAxis()
{
	// Draw a coordinate axis
	glColor3d(0., 1., 1.);

	glBegin(GL_LINES);
	glVertex3d(0., 0., 0.);
	glVertex3d(12., 0., 0.);
	glVertex3d(0., 0., 0.);
	glVertex3d(0., 12., 0.);
	glVertex3d(0., 0., 0.);
	glVertex3d(0., 0., 12.);
	glEnd();
}
void CChildView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	double rad_angle = (3.141592654f * _heli.getRotation()/180);

	// TODO: Add your message handler code here and/or call default
	/*if (nChar == 37)        // left arrow
		_heli.setRotation(5);
	if (nChar == 39)        // right
		_heli.setRotation(-5);
	if (nChar == 38)        // up, go forward
		_heli.setPosition(-(.2*sin(rad_angle)),0,-(.2*cos(rad_angle)));

	if (nChar == 40)        // down, go backwards
		_heli.setPosition((.2*sin(rad_angle)),0,(.2*cos(rad_angle)));
	if (nChar == 87)        // w, go up
		_heli.setPosition(0,.2,0.);
	if (nChar == 83)        // s, go down
		_heli.setPosition(0,-.2,0.);

	if (nChar == 65)        // a, strate left
		_heli.setPosition(-(.2*cos(rad_angle)),0,(.2*sin(rad_angle)));
	if (nChar == 68)        // d, strafe right
		_heli.setPosition((.2*cos(rad_angle)),0,-(.2*sin(rad_angle)));


	BaseCheck();

	COpenGLWnd::OnKeyDown(nChar, nRepCnt, nFlags);*/
	Invalidate();
}

void CChildView::BaseCheck()
{
	CGrVector pos = _heli.getPosition();
	std::wstringstream wss;
	wss << _score;
	if(pos[0]<3 && pos[1]<1 &&pos[2] <3)
	{
		_score++;
		wss<<"inside region   ";
	}	
	wss << _score<<"pos2"<<pos[2];


	OutputDebugString(wss.str().c_str());
	OutputDebugString(L"\n");

}

void CChildView::OnTimer(UINT_PTR nIDEvent)
{
	OutputDebugString(L"gearfest\n");

	COpenGLWnd::OnTimer(nIDEvent);
}

void CChildView::OnFileRendergame()
{
	_renderGame = !_renderGame;
	m_particles.SetEmission(true);
	m_particles.SetBounce(2);
}

void CChildView::OnUpdateFileRendergame(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(_renderGame);
}
